const util = require('../../../utils/util.js')

Page({
  data: {
    src: '../../assets/a.jpg',
    baiduAiToken:'',
    result: '',
    ifFront: true,
    cameraPosition: 'front'
  },
  takePhoto() {
    const ctx = wx.createCameraContext()
    ctx.takePhoto({
      quality: 'high',
      success: (res) => {
        console.log(res.tempImagePath);
        this.setData({
          src: res.tempImagePath
        })
        let base64 = wx.getFileSystemManager().readFileSync(res.tempImagePath, "base64")
        console.log(base64);
        let reqData={
          image_type: 'BASE64',
          image: base64,
          group_id_list: 'yanfabu'
        };
        util.searchFace(this.baiduAiToken,reqData).then((res) => {
          let resData = res.data;
          console.log(resData);
          if (resData.error_msg == 'SUCCESS'){
            let user_id = resData.result.user_list[0].user_id;
            let score = resData.result.user_list[0].score;
            this.setData({
              result: '用户：'+user_id+' 相似度：'+score
            })
          }else{
            this.setData({
              result: '未检索出该生物'
            })
          }
        });
      }
    })
  },
  changeCamera(){
    this.ifFront = !this.ifFront;
    if (this.ifFront){
      this.setData({
        cameraPosition: 'front'
      })
    }else{
      this.setData({
        cameraPosition: 'back'
      })
    }
    console.log(this.data.cameraPosition);
  },
  error(e) {
    console.log(e.detail)
  },
  onLoad: function () {
    console.log('进入camera页面');
    util.getBaiduToken().then((res) => {
      let token = res.data.access_token;
      this.baiduAiToken = token;
      console.log(token);
    });
  }
})